<?php

namespace Home\Controller;

use MongoDB\Driver\Manager;
use Think\Controller;
use PHPExcel_IOFactory;
use PHPExcel;

class FlowchannelController extends CommonController {

    public function loan(){
        $this->display('Loan:loan');
    }
    /*
     * 平台渠道列表
     */
    public function platform_list(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        //$get = $this->jiemi($get);
        $page=empty($get['page'])?0:$get['page'];
        $lime=empty($get['lime'])?6:$get['lime'];

        if(!empty($get['search'])){
            $search=$get['search'];
            $data1['instr(name,"'.$search.'")']=['gt',0];//模糊查询
        }

        //dt_flow_channel
        //dt_application_record
//        $merchant=M('dt_flow_channel','','DB2')
//            ->join('fc left join dt_application_record ar on fc.channel=ar.channel')
//            ->order('fc.ctime desc')
//            ->limit($page,$lime)
//            ->field('fc.id,fc.name,fc.url,fc.status,fc.ctime')
//            ->fetchSql(true)
//            ->select();
        $merchant=M('dt_flow_channel','','DB2')
            //->where(['channel'=>$get['channel']])
                ->where($data1)
            ->order('ctime desc')
            ->limit($page,$lime)
            ->field('id,name,url,status,ctime,channel')
            ->select();
        foreach ($merchant as $key=>$value){
            $merchant[$key]['count_flow']=M('dt_application_record','','DB2')->where(['channel'=>$value['channel']])->count('id');
            $merchant[$key]['count_merchant']=M('dt_merchant_record','','DB2')->where(['channel'=>$value['channel'],'status'=>1])->count('id');
            switch ($value['status']){
                case 1:$merchant[$key]['status']='启用';
                    break;
                case 2:$merchant[$key]['status']='停用';
                    break;
            }
            $merchant[$key]['ctime']=date('Y-m-d H:i',$value['ctime']);
        }
        $data['ret']='1001';
        $data['code']='success';
        $data['is_null']=empty($merchant)?1:0;//1没数据0有数据
        $data['page']=$page+count($merchant);
        $data['count']=ceil(M('dt_flow_channel','','DB2')->where($data1)->count('id')/$lime);
        $data['data']=$merchant;
        //$data = $this->jiami($data);
        $this->return = ['data'=>$data];
        return true;
    }
    /*
     * 渠道下商户列表
     */
    public function channel_merchant_list(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        $page=empty($get['page'])?0:$get['page'];
        $lime=empty($get['lime'])?6:$get['lime'];
        if(empty($get['channel'])){
            $data['ret']='1002';
            $data['code']='请选择渠道商';
            $this->return = ['data'=>$data];
            return false;
        }
        if(!empty($get['search'])){
            $search=$get['search'];
            $data1['instr(name,"'.$search.'")']=['gt',0];//模糊查询
        }
        // $dt_merchant_list=M('dt_merchant_list','','DB2')->where(['channel'=>$get['channel']])->order('ctime desc')->limit($page,$lime)->field('id,logo,name,url,slogan,ios,android,ctime,type,demand_flow,added_time,obtained_timea,shop_identifier')->select();
        $dt_merchant_list=M('dt_merchant_record','','DB2')->where($data1)->where(['channel'=>$get['channel'],'status'=>1])->order('ctime desc')->limit($page,$lime)->field('channel,shop_identifier,type,demand_flow,flow_price,add_time,added_time,obtained_time,status,ctime')->select();
        //print_r($dt_merchant_list);die;
        foreach ($dt_merchant_list as $key=>$value){
            $dt_merchant_list[$key]['ctime']=date('Y-m-d H:i',$value['ctime']);
            $dt_merchant_list[$key]['added_time']=date('Y-m-d H:i',$value['added_time']);
            $dt_merchant_list[$key]['obtained_time']=date('Y-m-d H:i',$value['obtained_time']);
            $merchant = M('dt_merchant_list','','DB2')->where(['shop_identifier'=>$value['shop_identifier']])->field('logo,name,slogan,ios,android')->select();
            //print_r($merchant);die;
            foreach ($merchant as $kk=>$vv){
            $dt_merchant_list[$key]['logo'] = $vv['logo'];//商户logo
            $dt_merchant_list[$key]['name'] = $vv['name'];//商户名
            $dt_merchant_list[$key]['slogan'] = $vv['slogan'];//商户标语
            $dt_merchant_list[$key]['url'] = $merchant['url'];
            $dt_merchant_list[$key]['ios'] = $vv['ios'];//ISO下载地址
            $dt_merchant_list[$key]['android'] = $vv['android'];//安卓下载地址
            $dt_merchant_list[$key]['actual_flow']=M('dt_application_record','','DB2')->where(['channel'=>$value['channel'],'shop_identifier'=>$value['shop_identifier'],'ctime'=>['gt',$value['add_time']]])->count('id');
            }
        }
        $data['ret']='1001';
        $data['code']='success';
        $data['is_null']=empty($dt_merchant_list)?1:0;//1没数据0有数据
        $data['page']=$page+count($dt_merchant_list);
        $data['count']=ceil(M('dt_merchant_record','','DB2')->where($data1)->where(['channel'=>$get['channel'],'status'=>1])->count('id')/$lime);
        $data['data']=$dt_merchant_list;
        //$data = $this->jiami($data);
        $this->return = ['data'=>$data];
        return true;

    }
    /*
     * APP商户列表
     */
    public function app_merchant_list(){
        header("Access-Control-Allow-Origin: *");
        $channel = I('channel');
        //$get['channel']='ab8e64795ecb14dec842cf6a9ebc0fe9';
        //$channel='ab8e64795ecb14dec842cf6a9ebc0fe9';
        if(empty($channel)){
            $data['ret']='1002';
            $data['code']='渠道号为空';
            $this->return = ['data'=>$data];
            return false;
        }
        $dt_flow_channel=M('dt_flow_channel','','DB2')->where(['channel'=>$channel,'status'=>1])->getField('id');
        if(empty($dt_flow_channel)){
            $data['ret']='1002';
            $data['code']='渠道异常';
            $this->return = ['data'=>$data];
            return false;
        }
        //$get = $this->jiemi($get);
        // $get['channel']='123';
        // $dt_merchant_list=M('dt_merchant_list','','DB2')->where(['channel'=>$get['channel']])->order('ctime desc')->field('logo,name,slogan,shop_identifier')->select();
//        $dt_merchant_ = M('dt_merchant_record', '', 'DB2')->where(['channel' => $channel, 'status' => 1,'type'=>2])->order('ctime desc')->field('shop_identifier,added_time,obtained_time')->select();
//        foreach ($dt_merchant_ as $kk=>$vv) {
//            $where = M('dt_merchant_record', '', 'DB2')->where(['status' => 1, 'channel' => $channel, 'added_time'=>['EGT',$vv['added_time']],'obtained_time'=>['ELT',$vv['obtained_time']]])->group('id')->field('shop_identifier')->select(false);
            //$ctime=time();
        //"channel="."'$channel'"." and status=1 and type=1 or added_time>$ctime and obtained_time<$ctime"
        //$where['(case when type=2 then(added_time>'.$ctime.' and obtained_time<'.$ctime.') else 0 end)']=['gt',0];
        //$dt_merchant_list = M('dt_merchant_record', '', 'DB2')->where(['status'=>1,'channel'=>$channel])->where($where)->order('ctime desc')->field('shop_identifier')->fetchSql(true)->select();
            //print_r($dt_merchant_list);
           // die;
        $dt_merchant_list = M('dt_merchant_record', '', 'DB2')->where(['status'=>1,'channel'=>$channel])->where(['status'=>1,'channel'=>$channel])->order('ctime desc')->field('shop_identifier,url')->select();
        foreach ($dt_merchant_list as $key => $value) {
                $merchant = M('dt_merchant_list', '', 'DB2')->where(['shop_identifier' => $value['shop_identifier'], 'status' => 1])->field('logo,name,slogan')->find();
                $dt_merchant_list[$key]['logo'] = $merchant['logo'];//logo
                $dt_merchant_list[$key]['name'] = $merchant['name'];//商户名
                $dt_merchant_list[$key]['slogan'] = $merchant['slogan'];//标语
                $dt_merchant_list[$key]['count'] = M('dt_application_record', '', 'DB2')->where(['shop_identifier' => $value['shop_identifier']])->count('id');
                $dt_merchant_list[$key]['count'] += 10000;
            }
        $data['ret']='1001';
        $data['code']='success';
        $data['is_null']=empty($dt_merchant_list)?1:0;//1没数据0有数据
        //$data['page']=$page+count($merchant);
        $data['data']=$dt_merchant_list;
        //$data = $this->jiami($data);
        $this->return = ['data'=>$data];
    }
    /*
     * APP商户列表
     */
    public function new_app_merchant_list(){
        header("Access-Control-Allow-Origin: *");
        $channel = I('channel');
        //$get['channel']='ab8e64795ecb14dec842cf6a9ebc0fe9';
        //$channel='ab8e64795ecb14dec842cf6a9ebc0fe9';
        if(empty($channel)){
            $data['ret']='1002';
            $data['code']='渠道号为空';
            $this->return = ['data'=>$data];
            return false;
        }
        $dt_flow_channel=M('dt_flow_channel','','DB2')->where(['channel'=>$channel,'status'=>1])->getField('id');
        if(empty($dt_flow_channel)){
            $data['ret']='1002';
            $data['code']='渠道异常';
            $this->return = ['data'=>$data];
            return false;
        }
        //$get = $this->jiemi($get);
        // $get['channel']='123';
        // $dt_merchant_list=M('dt_merchant_list','','DB2')->where(['channel'=>$get['channel']])->order('ctime desc')->field('logo,name,slogan,shop_identifier')->select();
//        $dt_merchant_ = M('dt_merchant_record', '', 'DB2')->where(['channel' => $channel, 'status' => 1,'type'=>2])->order('ctime desc')->field('shop_identifier,added_time,obtained_time')->select();
//        foreach ($dt_merchant_ as $kk=>$vv) {
//            $where = M('dt_merchant_record', '', 'DB2')->where(['status' => 1, 'channel' => $channel, 'added_time'=>['EGT',$vv['added_time']],'obtained_time'=>['ELT',$vv['obtained_time']]])->group('id')->field('shop_identifier')->select(false);
        //$ctime=time();
        //"channel="."'$channel'"." and status=1 and type=1 or added_time>$ctime and obtained_time<$ctime"
        //$where['(case when type=2 then(added_time>'.$ctime.' and obtained_time<'.$ctime.') else 0 end)']=['gt',0];
        //$dt_merchant_list = M('dt_merchant_record', '', 'DB2')->where(['status'=>1,'channel'=>$channel])->where($where)->order('ctime desc')->field('shop_identifier')->fetchSql(true)->select();
        //print_r($dt_merchant_list);
        // die;
        $dt_merchant_list = M('dt_merchant_record', '', 'DB2')->where(['status'=>1,'channel'=>$channel])->where(['status'=>1,'channel'=>$channel])->order('ctime desc')->field('shop_identifier')->select();
        foreach ($dt_merchant_list as $key => $value) {
            $merchant = M('dt_merchant_list', '', 'DB2')->where(['shop_identifier' => $value['shop_identifier'], 'status' => 1])->field('logo,name,slogan')->find();
            $dt_merchant_list[$key]['logo'] = $merchant['logo'];//logo
            $dt_merchant_list[$key]['name'] = $merchant['name'];//商户名
            $dt_merchant_list[$key]['slogan'] = $merchant['slogan'];//标语
            $dt_merchant_list[$key]['count'] = M('dt_application_record', '', 'DB2')->where(['shop_identifier' => $value['shop_identifier']])->count('id');
            $dt_merchant_list[$key]['count'] += 10000;
        }
        $data['ret']='1001';
        $data['code']='success';
        $data['is_null']=empty($dt_merchant_list)?1:0;//1没数据0有数据
        //$data['page']=$page+count($merchant);
        $data['data']=$dt_merchant_list;
        //$data = $this->jiami($data);
        $this->return = ['data'=>$data];
    }
    /*
     * web商户列表
     */
    public function web_merchant_list(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        $page=empty($get['page'])?0:$get['page'];
        $lime=empty($get['lime'])?6:$get['lime'];
        if(!empty($get['search'])){
            $search=$get['search'];
            $data1['instr(name,"'.$search.'")']=['gt',0];//模糊查询
//            $data['ret']='1001';
//            $data['code']=$search;
//            $this->return = ['data'=>$data];
//            return true;
        }
        $dt_merchant_list=M('dt_merchant_list','','DB2')->where($data1)->order('ctime desc')->limit($page,$lime)->field('id,name,logo,slogan,shop_identifier,android,ios,ctime')->select();
        foreach ($dt_merchant_list as $key=>$value){
            $dt_merchant_list[$key]['ctime']=date('Y-m-d H:i',$value['ctime']);
        }
        $data['ret']='1001';
        $data['code']='success';
        $data['is_null']=empty($dt_merchant_list)?1:0;//1没数据0有数据
        $data['page']=$page+count($dt_merchant_list);
        $data['count']=ceil(M('dt_merchant_list','','DB2')->where($data1)->count('id')/$lime);
        $data['data']=$dt_merchant_list;
        //$data = $this->jiami($data);
        $this->return = ['data'=>$data];
        return true;
    }
    /*
     * 商户修改
     */
    public function web_updata_merchant(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        if(empty($get['id'])){
            $data['ret']='1002';
            $data['code']='请选择修改渠道';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['name'])){
            $data['ret']='1002';
            $data['code']='渠道名不能为空';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['logo'])){
            $data['ret']='1002';
            $data['code']='渠道地址不能为空';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['ios'])){
            $data['ret']='1002';
            $data['code']='请选择状态';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['android'])){
            $data['ret']='1002';
            $data['code']='请选择状态';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['slogan'])){
            $data['ret']='1002';
            $data['code']='请选择状态';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        $data['name']=$get['name'];//商户名
        $data['slogan']=$get['slogan'];//标语
        $data['android']=$get['android'];//安卓下载地址
        $data['ios']=$get['ios'];//ios
        $data['logo']=$get['logo'];//logo
        $data['utime']=time();
        $dt_merchant_list=M('dt_merchant_list','','DB2')->where(['id'=>$get['id']])->save($data);
        if($dt_merchant_list){
            $data['ret']='1001';
            $data['code']='success';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return true;
        }else{
            $data['ret']='1002';
            $data['code']='修改失败';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }

    }
    /*
     * 申请
     */
    public function application(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        //$get = $this->jiemi($get);
        $get['user_mobile']=I('user_mobile');
        $get['mobile']=I('mobile');
        $get['shop_identifier']=I('shop_identifier');
        $get['channel']=I('channel');
//        $data['ret']='1001';
//        $data['code']='成功';
//        $data['data']=$get;
//        //$data = $this->jiami($data);
//        $this->return = ['data'=>$data];
//        return true;
//        return $get;die;
        if(empty($get['user_mobile'])){
            $data['ret']='1002';
            $data['code']='请先登录';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['shop_identifier'])){
            $data['ret']='1002';
            $data['code']='商户异常';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['channel'])){
            $data['ret']='1002';
            $data['code']='渠道异常';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        $data_url=M('dt_merchant_list','','DB2')->where(['shop_identifier'=>$get['shop_identifier']])->field('android,ios')->find();
        //$data_url['ios']='www.baidu.com';
        //$data_url['android']='http://app.qq.com/#id=detail&appid=1101070898';
        if(!preg_match("/1[345678]{1}\d{9}$/",$get['mobile'])){
            $data['ret']='1002';
            $data['code']='手机号格式不正确';
            $data['data']=$data_url;
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        $data7['user_mobile']=$get['user_mobile'];//登录的手机号
        $data7['mobile']=$get['mobile'];//手机号
        $data7['channel']=$get['channel'];//渠道号
        $data7['shop_identifier']=$get['shop_identifier'];//商户号
        $data7['ctime']=time();//创建时间
        $data7['utime']=time();//修改时间
        $data7['ip']=$_SERVER["REMOTE_ADDR"];//IP
        $user=M('dt_application_record','','DB2')->where(['shop_identifier'=>$get['shop_identifier'],'ip'=>$data7['ip']])->field('id')->find();
        if(!empty($user)){
            $data['ret']='1001';
            $data['code']='申请成功';
            $data['data']=$data_url;
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        $dt_merchant=M('dt_merchant_record','','DB2')->where(['channel'=>$get['channel'],'shop_identifier'=>$get['shop_identifier'],'status'=>1])->field('demand_flow,add_time')->find();
        $count_merchant_record=M('dt_application_record','','DB2')->where(['channel'=>$get['channel'],'shop_identifier'=>$get['shop_identifier'],'ctime'=>['gt',$dt_merchant['add_time']]])->count('id');
        $count_merchant_record+=1;
        if($count_merchant_record>=$dt_merchant['demand_flow']){
            $dt_merchant_updata=M('dt_merchant_record','','DB2')->where(['channel'=>$get['channel'],'shop_identifier'=>$get['shop_identifier'],'status'=>1])->save(['status'=>2,'utime'=>time(),'obtain_time'=>time()]);

        }
        $dt_application_record=M('dt_application_record','','DB2')->add($data7);
        if($dt_application_record){
            $data['ret']='1001';
            $data['code']='申请成功';
            $data['data']=$data_url;
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return true;
        }else{
            $data['ret']='1002';
            $data['code']='申请失败';
            $data['data']=$data_url;
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
    }
    /*
     * 创建渠道
     */
    public function add_channel(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        if(empty($get['name'])){
            $data['ret']='1002';
            $data['code']='渠道名为空';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['url'])){
            $data['ret']='1002';
            $data['code']='渠道连接为空';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['status'])){
            $data['ret']='1002';
            $data['code']='请选择开启或关闭';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }

        $data7['name']=$get['name'];//渠道名
        $data7['url']=$get['url'];//渠道链接
        $data7['status']=$get['status'];//1启动2停用
        $data7['ctime']=time();//创建时间
        $data7['utime']=time();//创建时间
        $data7['channel']=md5($get['name'].$data7['ctime']);

        $dt_flow_channel=M('dt_flow_channel','','DB2')->add($data7);
        if($dt_flow_channel){
            $data['ret']='1001';
            $data['code']='success';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return true;
        }else{
            $data['ret']='1002';
            $data['code']='添加失败';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return true;
        }
    }
    /*
     * 修改渠道
     */
    public function updata_channel()
    {
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        if(empty($get['id'])){
            $data['ret']='1002';
            $data['code']='请选择修改渠道';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['name'])){
            $data['ret']='1002';
            $data['code']='渠道名不能为空';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['url'])){
            $data['ret']='1002';
            $data['code']='渠道地址不能为空';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['status'])){
            $data['ret']='1002';
            $data['code']='请选择状态';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        $data['name']=$get['name'];
        $data['url']=$get['url'];
        $data['status']=$get['status'];
        $dt_flow_channel=M('dt_flow_channel','','DB2')->where(['id'=>$get['id']])->save($data);
        if($dt_flow_channel){
            $data['ret']='1001';
            $data['code']='success';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return true;
        }else{
            $data['ret']='1002';
            $data['code']='修改失败';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }



    }
    /*
     * 商户列表选择框
     */
    public function merchant_list_select(){
        header("Access-Control-Allow-Origin: *");
        $dt_merchant_list=M('dt_merchant_list','','DB2')->where(['status'=>1])->field('id,name,logo,ios,android,slogan,shop_identifier')->select();
        $data['ret']='1001';
        $data['code']='success';
        $data['is_null']=empty($dt_merchant_list)?1:0;//1没数据0有数据
        $data['data']=$dt_merchant_list;
        //$data = $this->jiami($data);
        $this->return = ['data'=>$data];
        return true;
    }
    /*
     * 渠道添加商户
     */
    public function channel_add_merchant(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        if(empty($get['id'])){
            $data['ret']='1002';
            $data['code']='请选择商户';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['channel'])){
            $data['ret']='1002';
            $data['code']='渠道号为空';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['shop_identifier'])){
            $data['ret']='1002';
            $data['code']='商户为空';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['url'])){
            $data['ret']='1002';
            $data['code']='注册渠道连接为空';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        $dt_merchant_record=M('dt_merchant_record','','DB2')->where(['channel'=>$get['channel'],'shop_identifier'=>$get['shop_identifier'],'status'=>1])->field('id,name')->find();
        if($dt_merchant_record['id']){
            $data['ret']='1002';
            $data['code']='不能重复添加商户';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        $name=M('dt_merchant_list','','DB2')->where(['shop_identifier'=>$get['shop_identifier'],'status'=>1])->getField('name');
        $data['name']=$name;//商户名
        $data['channel']=$get['channel'];//渠道号
        $data['type']=$get['type'];//1定量2定时
        $data['ctime']=time();
        $data['utime']=time();
        $data['add_time']=$data['ctime'];//实际上架时间
        $data['shop_identifier']=$get['shop_identifier'];//商户号
        $data['url']=$get['url'];//渠道注册连接
        $data['flow_price']=empty($get['flow_price'])?15:$get['flow_price'];//流量单价
        if($data['type']==1){
            if(empty($get['demand_flow'])){
                $data['ret']='1002';
                $data['code']='计划流量数为空';
                $this->return = ['data'=>$data];
                return false;
            }else{
                $data['demand_flow'] = $get['demand_flow'];
            }
        }else{
            if(empty($get['added_time'])){
                $data['ret']='1002';
                $data['code']='上架时间为空';
                $this->return = ['data'=>$data];
                return false;
            }
            if(empty($get['obtained_timea'])){
                $data['ret']='1002';
                $data['code']='下架时间为空';
                $this->return = ['data'=>$data];
                return false;
            }
            $data['added_time'] = strtotime($get['added_time']);
            $data['obtained_timea'] = strtotime($get['obtained_timea']);
        }
        //$dt_merchant_list=M('dt_merchant_list','','DB2')->where(['id'=>$get['id']])->save($data);
        $dt_merchant_list=M('dt_merchant_record','','DB2')->add($data);
        //$dt_merchant_list=M('dt_merchant_record','','DB2')->where(['channel'=>$get['channel'],'shop_identifier'=>$get['shop_identifier']])->add($data);
//        $data7['channel']=$get['channel'];
//        $data7['shop_identifier']=$get['shop_identifier'];
//        $data7['type']=$get['type'];
//        $data7['demand_flow']=$get['demand_flow'];
//        $data7['added_time']=$get['added_time'];
//        $data7['obtained_time']=$get['obtained_time'];
//        $data7['flow_price']=$get['flow_price'];//流量单价
//        $data7['status']=1;//1启用2禁用3删除
//        $data7['ctime']=time();
//        $data7['utime']=$data7['ctime'];
//        $dt_merchant_record=M('dt_merchant_record','','DB2')->add($data7);

        if($dt_merchant_list){

            $data['ret']='1001';
            $data['code']='success';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return true;
        }else{
            $data['ret']='1002';
            $data['code']='修改失败';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
    }
    /*
     * 渠道下架商户
     */
    public function channel_obtained_merchant()
    {
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        if(empty($get['id'])){
            $data['ret']='1002';
            $data['code']='请选择下架商户';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        $data7['status']=2;

        $data7['utime']=time();
        $data7['obtain_time']=time();//实际下架时间
        $dt_merchant_record=M('dt_merchant_record','','DB2')->where(['id'=>$get['id']])->save($data7);
        if($dt_merchant_record){
            $data['ret']='1001';
            $data['code']='success';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return true;
        }else{
            $data['ret']='1002';
            $data['code']='下架失败';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
    }
    /*
     * 下架商户
     */
    public function obtained_merchant(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        if(empty($get['shop_identifier'])){
            $data['ret']='1002';
            $data['code']='请选择下架商户';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['channel'])){
            $data['ret']='1002';
            $data['code']='渠道异常';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        $data['utime']=time();
        $data['obtain_time']=time();
        $data['status']=2;//1正常2下架
        // $dt_merchant_list=M('dt_merchant_list','','DB2')->where(['id'=>$get['id']])->save($data);
        $dt_merchant_list=M('dt_merchant_record','','DB2')->where(['channel'=>$get['channel'],'shop_identifier'=>$get['shop_identifier'],'status'=>1])->save($data);
        if($dt_merchant_list){
            $data['ret']='1001';
            $data['code']='success';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return true;
        }else{
            $data['ret']='1002';
            $data['code']='下架失败';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
    }
    /*
     * 上架商户
     */
    // public function add_merchant(){

    // }
    /*
     * 上架商户列表
     */
    public function merchant_list(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        $page=empty($get['page'])?0:$get['page'];
        $lime=empty($get['lime'])?6:$get['lime'];
        if(!empty($get['search'])){
            $search=$get['search'];
            $data1['instr(name,"'.$search.'")']=['gt',0];//模糊查询
        }
        $dt_merchant_list=M('dt_merchant_list','','DB2')->where($data1)->order('ctime desc')->limit($page,$lime)->field('id,logo,name,ctime,shop_identifier,channel,flow_price,demand_flow,type')->group('name')->select();
        foreach ($dt_merchant_list as $key=>$value){
            $dt_merchant_list[$key]['ctime']=date('Y-m-d H:i',$value['ctime']);
            switch ($value['type']){
                case 1:$dt_merchant_list[$key]['type']='定量';
                    break;
                case 2:$dt_merchant_list[$key]['type']='定时';
                    break;

            }
            $dt_merchant_list[$key]['flow_count']=M('dt_application_record','','DB2')->where(['shop_identifier'=>$value['shop_identifier']])->count('id');//流量总数
            $dt_merchant_list[$key]['merchant_count']=M('dt_merchant_list','','DB2')->join('ml join dt_flow_channel dc on ml.channel=dc.channel')->where(['dc.status'=>1,'ml.shop_identifier'=>$value['shop_identifier']])->count('ml.id');//上架渠道数

//            $record = M('dt_merchant_record','','DB2')->where(['shop_identifier'=>$value['shop_identifier'],'status'=>1])->field('channel')->select();
//            $channel_name = '';
//            foreach($record as $k=>$v){
//                $name = M('dt_flow_channel','','DB2')->where(['channel'=>$value['channel']])->getField('name');
//                $channel_name .= $name." ";
//            }
//            $dt_merchant_list[$key]['merchant'] = $channel_name;
             $dt_merchant_list[$key]['merchant']=M('dt_merchant_list','','DB2')->join('ml join dt_flow_channel dc on ml.channel=dc.channel')->where(['dc.status'=>1,'ml.shop_identifier'=>$value['shop_identifier']])->order('dc.ctime desc')->field('dc.name')->select();
             //$dt_merchant_list[$key]['merchant_count']=empty($dt_merchant_list[$key]['merchant_count'])?0:$dt_merchant_list[$key]['merchant_count'];//null处理
        }
        $data['ret']='1001';
        $data['code']='success';
        $data['is_null']=empty($dt_merchant_list)?1:0;//1没数据0有数据
        $data['page']=$page+count($dt_merchant_list);
        $data['count']=ceil(M('dt_merchant_list','','DB2')->where($data1)->count('id')/$lime);
        $data['data']=$dt_merchant_list;
        //$data = $this->jiami($data);
        $this->return = ['data'=>$data];
        return true;
    }
    /*
     * 新增商户
     */
    public function add_merchant(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');        
        if(empty($get['merchant_name'])){
            $data['ret']='1002';
            $data['code']='商户名为空';
            $this->return = ['data'=>$data];
            return false;
        }
//        if(empty($get['shop_identifier'])){
//            $data['ret']='1002';
//            $data['code']='商户号为空';
//            $this->return = ['data'=>$data];
//            return false;
//        }
        if(empty($get['slogan'])){
            $data['ret']='1002';
            $data['code']='商户标语为空';
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['app_url'])){
            $data['ret']='1002';
            $data['code']='安卓下载地址为空';
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['ios_url'])){
            $data['ret']='1002';
            $data['code']='IOS下载地址为空';
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['logo'])){
            $data['ret']='1002';
            $data['code']='LOGO为空';
            $this->return = ['data'=>$data];
            return false;
        }
//        $has_merchant = M('merchant','','DB2')->where(array('shop_identifier'=>$get['shop_identifier']))->find();
//        if(!$has_merchant){
//            $data['ret']='1002';
//            $data['code']='商户号无匹配';
//            $this->return = ['data'=>$data];
//            return false;
//        }
//        $exist = M('dt_merchant_list','','DB2')->where(array('shop_identifier'=>$get['shop_identifier']))->find();
//        if($exist){
//            $data['ret']='1002';
//            $data['code']='商户已存在';
//            $this->return = ['data'=>$data];
//            return false;
//        }
        $Mdata['name'] = $get['merchant_name'];//商户名
        $Mdata['shop_identifier'] ='01000'.time().rand(1111,9999);
        $Mdata['slogan'] = $get['slogan'];//标语
        $Mdata['android'] = $get['app_url'];//安卓下载地址
        $Mdata['ios'] = $get['ios_url'];//ios下载地址
        $Mdata['logo'] = $get['logo'];//商户logo
        $Mdata['ctime'] = time();
        $Mdata['utime'] = time();
        $Mdata['status'] =1;
        $result = M('dt_merchant_list','','DB2')->add($Mdata);
        if($result){
            $data['ret']='1001';
            $data['code']='success';
            $this->return = ['data'=>$data];
            return true;
        }else{
            $data['ret']='1002';
            $data['code']='添加失败';
            $this->return = ['data'=>$data];
            return false;
        }
    }


    public function export(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        $get=htmlspecialchars_decode($get);
        $get=json_decode($get,true);
        //$get=json_decode($get,true);
        $merchant = $get['merchant'];
        //echo $get;
//        print_r($get);die;
        //$merchant='0100010020,0100010019';
//        $get['starttime']=date('Y-m-d H:i');
//        $get['endtime']=date('Y-m-d H:i');
        if(empty($merchant)){
            $data['ret']='1002';
            $data['code']='未选择商户';
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['starttime'])||empty($get['endtime'])){
            $data['ret']='1002';
            $data['code']='未选择时间';
            $this->return = ['data'=>$data];
            return false;
        }

        $start = strtotime($get['starttime']);
        $end = strtotime($get['endtime']);
        $where["ctime"]=array('between',array($start,$end));
        $merchant_Arr = explode(',',$merchant);
        $array = array();
        $dt_application_record = M('dt_application_record','','DB2');
        foreach($merchant_Arr as $v){
            $where['shop_identifier'] = $v;
            $record_list = $dt_application_record->where($where)->select();
            //print_r($record_list);die;
            foreach($record_list as $kk=>$vv){
                $dt_channel = M('dt_flow_channel','','DB2')->where(['channel'=>$vv['channel'],'status'=>1])->getField('name');
                $merchant_name = M('dt_merchant_list','','DB2')->where(['shop_identifier'=>$vv['shop_identifier']])->getField('name');
                $record_list[$kk]['channel_name'] = $dt_channel;
                $record_list[$kk]['merchant_name'] = $merchant_name;
                $record_list[$kk]['ctime'] = date('Y-m-d H:i:s',$vv['ctime']);
            }
            $record_list['title'] = M('dt_merchant_list','','DB2')->where(['shop_identifier'=>$v])->getField('name');
            $array[] = $record_list;
        }
        //print_r($array);die;
        $this->exportExcel($array);

    }
    /*
     * 全部上架记录
     */
    public function upper_record_all(){
        $get = I('data');
        $page=empty($get['page'])?0:$get['page'];
        $lime=empty($get['lime'])?6:$get['lime'];
        if(!empty($get['search'])){
            //$search=$get['search'];
            $data1['instr(name,"'.$get['search'].'")']=['gt',0];//模糊查询
        }
        $dt_merchant_list=M('dt_merchant_record','','DB2')->where($data1)->field('id,channel,type,demand_flow,add_time,obtain_time')->limit($page,$lime)->select();
        foreach ($dt_merchant_list as $key=>$value){

            $dt_merchant_list[$key]['channel_name']=M('dt_flow_channel','','DB2')->where(['channel'=>$value['channel']])->getfield('name');
            $dt_merchant_list[$key]['flow_count']=M('dt_application_record','','DB2')->where(['shop_identifier'=>$value['shop_identifier'],'channel'=>$value['channel']])->count('id');//实际流量总数
            if($value['type']==1){//流量差额
                $flow_diff = $value['demand_flow']-$dt_merchant_list[$key]['flow_count'];
                $dt_merchant_list[$key]['flow_diff'] = $flow_diff<0?0:$flow_diff;
            }else{
                $dt_merchant_list[$key]['flow_diff'] = '/';
            }
        }
        $channel_count = M('dt_merchant_record','','DB2')->where(['shop_identifier'=>$value['shop_identifier']])->count('id');//上架渠道数
        $flow_count = M('dt_application_record','','DB2')->where(['shop_identifier'=>$value['shop_identifier']])->count('id');//获取流量数
        $merchant['channel_count'] = $channel_count;
        $merchant['flow_count'] = $flow_count;
        $data['ret']='1001';
        $data['code']='success';
        $data['count']=ceil(M('dt_merchant_list','','DB2')->where($data1)->count('id')/$lime);
        $data['data']=array('dt_merchant_list'=>$dt_merchant_list,'merchant'=>$merchant);
        //$data = $this->jiami($data);
        $this->return = ['data'=>$data];
        return true;
    }
    /**
     * 商户上架记录
     * 
     */
    public function upper_record(){
        $get = I('data');
        $page=empty($get['page'])?0:$get['page'];
        $lime=empty($get['lime'])?10:$get['lime'];
        $shop_identifier = $get['shop_identifier'];
        //$shop_identifier='0100010020';
        if(empty($shop_identifier)){
            $data['ret']='1002';
            $data['code']='商户异常';
            $this->return = ['data'=>$data];
            return true;
        }
        $where['shop_identifier'] = $shop_identifier;
        // $where['status'] = 1;
        if(!empty($get['search'])){
            $search=$get['search'];
            $data1['instr(name,"'.$search.'")']=['gt',0];//模糊查询
        }
        $dt_merchant_list=M('dt_merchant_record','','DB2')->where($data1)->field('id,channel,shop_identifier,type,demand_flow,add_time,added_time,obtained_time,obtain_time')->where($where)->limit($page,$lime)->order('ctime desc')->select();
        foreach ($dt_merchant_list as $key=>$value){
            //$dt_merchant_list[$key]['ctime']=empty($value['ctime'])?'':date('Y-m-d H:i',$value['ctime']);
            $dt_merchant_list[$key]['add_time']=empty($value['add_time'])?'':date('Y-m-d H:i',$value['add_time']);
            $dt_merchant_list[$key]['obtain_time']=empty($value['obtain_time'])?'':date('Y-m-d H:i',$value['obtain_time']);
            //$dt_merchant_list[$key]['obtain_time']=empty($value['obtain_time'])?'':date('Y-m-d H:i',$value['obtain_time']);
            $dt_merchant_list[$key]['obtained_time']=empty($value['obtained_time'])?'':date('Y-m-d H:i',$value['obtained_time']);
            $dt_merchant_list[$key]['added_time']=empty($value['added_time'])?'':date('Y-m-d H:i',$value['added_time']);

            $dt_merchant_list[$key]['channel_name']=M('dt_flow_channel','','DB2')->where(['channel'=>$value['channel']])->getfield('name');
            $dt_merchant_list[$key]['flow_count']=M('dt_application_record','','DB2')->where(['shop_identifier'=>$value['shop_identifier'],'channel'=>$value['channel'],'ctime'=>[['egt',$value['add_time']],['elt',$value['obtain_time']]]])->count('id');//实际流量总数
            if(empty($value['obtain_time'])){
                $dt_merchant_list[$key]['flow_count']=M('dt_application_record','','DB2')->where(['shop_identifier'=>$value['shop_identifier'],'channel'=>$value['channel'],'ctime'=>['egt',$value['add_time']]])->count('id');//实际流量总数

            }
            if($value['type']==1){//流量差额
                $flow_diff = $value['demand_flow']-$dt_merchant_list[$key]['flow_count'];
                $dt_merchant_list[$key]['flow_diff'] = $flow_diff<0?0:$flow_diff;
            }else{
                $dt_merchant_list[$key]['flow_diff'] = '/';
            }
        }
        $channel_count = M('dt_merchant_record','','DB2')->where(['shop_identifier'=>$shop_identifier])->count('id');//上架渠道数
        $flow_count = M('dt_application_record','','DB2')->where(['shop_identifier'=>$shop_identifier])->count('id');//获取流量数
        $merchant['channel_count'] = $channel_count;
        $merchant['flow_count'] = $flow_count;
        $data['ret']='1001';
        $data['code']='success';
        $data['count']=ceil(M('dt_merchant_record','','DB2')->where($data1)->where(['shop_identifier'=>$shop_identifier])->count('id')/$lime);
        $data['data']=array('dt_merchant_list'=>$dt_merchant_list,'merchant'=>$merchant);
        $data['page']=$page+count($dt_merchant_list);

        //$data = $this->jiami($data);
        $this->return = ['data'=>$data];
        return true;
    }
    /**
     * 商户上架记录
     *
     */
    public function ceshi(){
        $get = I('data');
        $page=empty($get['page'])?0:$get['page'];
        $lime=empty($get['lime'])?6:$get['lime'];
        $shop_identifier = $get['shop_identifier'];
        //$shop_identifier='0100010020';
        $shop_identifier='0100015389947731474';
        if(empty($shop_identifier)){
            $data['ret']='1002';
            $data['code']='商户异常';
            $this->return = ['data'=>$data];
            return true;
        }
        $where['shop_identifier'] = $shop_identifier;
        // $where['status'] = 1;
        if(!empty($get['search'])){
            $search=$get['search'];
            $data1['instr(name,"'.$search.'")']=['gt',0];//模糊查询
        }
        $dt_merchant_list=M('dt_merchant_record','','DB2')->where($data1)->field('id,channel,shop_identifier,type,demand_flow,add_time,added_time,obtained_time,obtain_time')->where($where)->limit($page,$lime)->order('ctime desc')->select();
        foreach ($dt_merchant_list as $key=>$value){
            //$dt_merchant_list[$key]['ctime']=empty($value['ctime'])?'':date('Y-m-d H:i',$value['ctime']);
            $dt_merchant_list[$key]['add_time']=empty($value['add_time'])?'':date('Y-m-d H:i',$value['add_time']);
            $dt_merchant_list[$key]['obtain_time']=empty($value['obtain_time'])?'':date('Y-m-d H:i',$value['obtain_time']);
            //$dt_merchant_list[$key]['obtain_time']=empty($value['obtain_time'])?'':date('Y-m-d H:i',$value['obtain_time']);
            $dt_merchant_list[$key]['obtained_time']=empty($value['obtained_time'])?'':date('Y-m-d H:i',$value['obtained_time']);
            $dt_merchant_list[$key]['added_time']=empty($value['added_time'])?'':date('Y-m-d H:i',$value['added_time']);

            $dt_merchant_list[$key]['channel_name']=M('dt_flow_channel','','DB2')->where(['channel'=>$value['channel']])->getfield('name');
            $dt_merchant_list[$key]['flow_count']=M('dt_application_record','','DB2')->where(['shop_identifier'=>$value['shop_identifier'],'channel'=>$value['channel'],'ctime'=>[['egt',$value['add_time']],['elt',$value['obtain_time']]]])->count('id');//实际流量总数
            $dt_merchant_list[$key]['ceshi']=$value['obtain_time'];
            //print_r($dt_merchant_list[$key]['flow_count']);die;
            if($value['type']==1){//流量差额
                $flow_diff = $value['demand_flow']-$dt_merchant_list[$key]['flow_count'];
                $dt_merchant_list[$key]['flow_diff'] = $flow_diff<0?0:$flow_diff;
            }else{
                $dt_merchant_list[$key]['flow_diff'] = '/';
            }
        }
        $channel_count = M('dt_merchant_record','','DB2')->where(['shop_identifier'=>$shop_identifier])->count('id');//上架渠道数
        $flow_count = M('dt_application_record','','DB2')->where(['shop_identifier'=>$shop_identifier])->count('id');//获取流量数
        $merchant['channel_count'] = $channel_count;
        $merchant['flow_count'] = $flow_count;
        $data['ret']='1001';
        $data['code']='success';
        $data['count']=ceil(M('dt_merchant_list','','DB2')->where($data1)->where(['shop_identifier'=>$shop_identifier])->count('id')/$lime);
        $data['data']=array('dt_merchant_list'=>$dt_merchant_list,'merchant'=>$merchant);
        $data['page']=$page+count($dt_merchant_list);

        //$data = $this->jiami($data);
        $this->return = ['data'=>$data];
        return true;
    }





    /**
     * [exportExcel description:多sheet导出Excel]
     * @ param array    $exportData 导出文档的数据
     * @return 通过浏览器输出
     */
    public function exportExcel($exportData){
        $array1 = $exportData[0];
        unset($exportData[0]);
        ini_set('memory_limit','1024M');            
        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel(); //创建PHPExcel对象
        //第一个sheet填入主标题
        $first_sheet_title = $array1['title'].'导流数据表';
        unset($array1['title']);
        $objPHPExcel->getActiveSheet()->setCellValue('A1', $first_sheet_title);
        //合并单元格
        $objPHPExcel->getActiveSheet()->mergeCells('A1:D1');
        //设置居中
        $objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getAlignment()
        ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
        ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
     
        //设置单元格宽
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(40);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(50);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);
        //字体加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('宋体') //字体
        ->setBold(true); //字体加粗
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setName('宋体') //字体
        ->setBold(true); //字体加粗
        $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->setName('宋体') //字体
        ->setBold(true); //字体加粗
        $objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->setName('宋体') //字体
        ->setBold(true); //字体加粗
        $objPHPExcel->getActiveSheet()->getStyle('D2')->getFont()->setName('宋体') //字体
        ->setBold(true); //字体加粗     
        //第一sheet
        $array = array(
                array("渠道名","商户号","创建时间","用户电话"),
        );        
        
        $data = array();
        if(is_array($array1)){
            foreach($array1 as $key=>$value){
                $data[$key][]=$value['channel_name'];
                $data[$key][]=$value['shop_identifier'];
                $data[$key][]=$value['ctime'];
                $data[$key][]=$value['mobile'];
            }
        }
        //合并title
        $new_arr = array_merge($array,$data);
     
        //写入到第一个sheet
        $k = 1;
        foreach ($new_arr as $val){
            $k = $k+1;
            $objPHPExcel->getactivesheet()->setcellvalue('A'.$k, $val[0]);//第A列 第$k行  
            $objPHPExcel->getactivesheet()->setcellvalue('B'.$k, $val[1]);
            $objPHPExcel->getactivesheet()->setcellvalue('C'.$k, $val[2]);
            $objPHPExcel->getactivesheet()->setcellvalue('D'.$k, $val[3]);
        }
     
        /*保存到第一个sheet*/
        $objPHPExcel->getActiveSheet()->setTitle($array1[0]['merchant_name']);
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);
        //第一sheet -end
     
     
        foreach($exportData as $key=>$value){
            //第二sheet        
            $second = $key;
            $sheet_title = $value['title'];            
            $A1_title = $value['title'];
            unset($value['title']);
            $A2_title = array(
                    array("渠道名","商户号","创建时间","用户电话"),
            );
            $exportDa = $value;
            $this->makeData($objPHPExcel,$second,$sheet_title,$A1_title,$A2_title,$exportDa);
            //写入到第二个sheet-end
        }        
        
        //最后通过浏览器输出
        ob_end_clean();
        $fn = '导流信息('.date('YmdHis').').xls';
        header('Content-Type: application/vnd.ms-excel;');
        header("Content-Disposition: attachment;filename=$fn");
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }





    /*
     * 添加多个sheet
     * @ $objPHPExcel   phpexcel 对象
     * @ $num  int       第n个工作空间
     * @ $sheet_title    工作空间sheet名称
     * @ param string   $A1_title 大标题
     * @ param array    $A2_title 每个字段名
     * @ exportDa array 导出文档的数据
     * @ return true or false;
     */
    protected  function makeData($objPHPExcel,$num,$sheet_title,$A1_title,$A2_title,$exportDa){
        $objPHPExcel->createSheet();
        $objPHPExcel->setActiveSheetIndex($num);
        $objPHPExcel->getActiveSheet()->setCellValue('A1', $A1_title.'导流数据表');
        //合并单元格
        $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
        //设置居中
        $objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getAlignment()
        ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
        ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        //设置单元格宽
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(40);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(50);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);
        //字体加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('宋体') //字体
        ->setBold(true); //字体加粗
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setName('宋体') //字体
        ->setBold(true); //字体加粗
        $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->setName('宋体') //字体
        ->setBold(true); //字体加粗
        $objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->setName('宋体') //字体
        ->setBold(true); //字体加粗
        $objPHPExcel->getActiveSheet()->getStyle('D2')->getFont()->setName('宋体') //字体
        ->setBold(true); //字体加粗
        
        
        $array_3 = $exportDa;
        $data = array();
        if(is_array($array_3)){
            foreach($array_3 as $key=>$value){
                $data[$key][]=$value['channel_name'];
                $data[$key][]=$value['shop_identifier'];
                $data[$key][]=$value['ctime'];
                $data[$key][]=$value['mobile'];
            }
        }
        $new_arr = array_merge($A2_title,$data);
        //写入到第n个sheet
        $i = 1;
        foreach ($new_arr as $val){
            $i = $i+1;
            $objPHPExcel->getactivesheet()->setcellvalue('A'.$i, $val[0]);//第A列 第$k行
            $objPHPExcel->getactivesheet()->setcellvalue('B'.$i, $val[1]);
            $objPHPExcel->getactivesheet()->setcellvalue('C'.$i, $val[2]);
            $objPHPExcel->getactivesheet()->setcellvalue('D'.$i, $val[3]);
        }
        $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007");
        /*保存到本地*/
        $objPHPExcel->getActiveSheet()->setTitle($sheet_title);
        return true;
    }
    /*
     * 登录
     */
    public function logn(){
        header("Access-Control-Allow-Origin: *");
        $get = I('data');

        if(!preg_match("/1[345678]{1}\d{9}$/",$get['mobile'])){
            $data['ret']='1002';
            $data['code']='手机号格式不正确';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        if(empty($get['pwd'])){
            $data['ret']='1002';
            $data['code']='密码为空';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
        $user=M('dt_user','','DB2')->where(['mobile'=>$get['mobile'],'status'=>1])->field('mobile,pwd')->find();
        if($user['pwd']==$get['pwd']&&$user['mobile']==$get['mobile']){
            $data['ret']='1001';
            $data['code']='登录成功';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return true;
        }else{
            $data['ret']='1002';
            $data['code']='账号或密码错误';
            //$data = $this->jiami($data);
            $this->return = ['data'=>$data];
            return false;
        }
    }
    /*
     * 获取手机号
     */
    public function get_user_mobile()
    {
        header("Access-Control-Allow-Origin: *");
        $get = I('data');
        $get['user_id'] = I('user_id');
        //$get['user_id'] ='06bdc63723808fb24475f214ec4a864e';
        if (empty($get['user_id'])) {
            $data['ret'] = '1002';
            $data['code'] = '请先登录';
            //$data = $this->jiami($data);
            $this->return = ['data' => $data];
            return false;
        }
        $user_mobile = M('user','','QXF')->where(['vid'=>$get['user_id'],'status'=>1])->field('mobile')->find();
        if ($user_mobile) {
            $data['ret'] = '1001';
            $data['code'] = 'success';
            $data['data'] = $user_mobile;
            //$data = $this->jiami($data);
            $this->return = ['data' => $data];
            return true;
        } else {
            $data['ret'] = '1002';
            $data['code'] = '数据异常';
            //$data = $this->jiami($data);
            $this->return = ['data' => $data];
            return false;
        }
    }
    /*
     *  贷款超市V2
     */
    public function activity(){
       $this->display('Activity/activity');
    }

}